package com.zgzg.shop.pay.business.fuiou.service.impl;

import com.zgzg.shop.pay.business.fuiou.service.FuiouPayManagerService;
import com.zgzg.shop.pay.common.bean.PayOrder;
import com.zgzg.shop.pay.fuiou.api.FuiouPayConfigStorage;
import com.zgzg.shop.pay.fuiou.api.FuiouPayService;
import com.zgzg.shop.pay.fuiou.bean.FuiouTransactionType;
import com.zgzg.shop.pay.web.support.HttpRequestNoticeParams;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;
import java.util.UUID;

@Service
public class FuiouPayManagerServiceImpl implements FuiouPayManagerService {

    @Override
    public FuiouPayService buildPayStorageConfig(String orderId) {
        FuiouPayConfigStorage fuiouPayConfigStorage = new FuiouPayConfigStorage();
        fuiouPayConfigStorage.setMchntCd("合作者id");
        fuiouPayConfigStorage.setKeyPublic("支付密钥");
        fuiouPayConfigStorage.setKeyPrivate("支付密钥");
        fuiouPayConfigStorage.setNotifyUrl("异步回调地址");
        fuiouPayConfigStorage.setReturnUrl("同步回调地址");
        fuiouPayConfigStorage.setSignType("MD5");
        fuiouPayConfigStorage.setInputCharset("utf-8");
        //是否为测试账号，沙箱环境
        fuiouPayConfigStorage.setTest(true);

        FuiouPayService  service = new FuiouPayService(fuiouPayConfigStorage);

        //设置回调消息处理
        //TODO {@link com.egzosn.pay.demo.controller.FuiouPayController#payBack}
//        service.setPayMessageHandler(new FuiouPayMessageHandler(null));
        return service;
    }

    @Override
    public String toPay(BigDecimal price,String orderId) {
        //支付订单基础信息
        PayOrder order = new PayOrder("订单title", "摘要",  BigDecimal.valueOf(0.01) , UUID.randomUUID().toString().replace("-", "").substring(2));
        order.setTransactionType(FuiouTransactionType.B2C);
        //获取支付所需的信息
//        Map<String, Object> directOrderInfo = service.orderInfo(order);
        //获取表单提交对应的字符串，将其序列化到页面即可,
//       return service.buildRequest(directOrderInfo, MethodType.POST);
        return this.buildPayStorageConfig(orderId).toPay(order);
    }

    @Override
    public String payBackBefore(HttpServletRequest request) throws IOException {

        FuiouPayService service =   this.buildPayStorageConfig(null);

        //获取支付方返回的对应参数
        Map<String, Object> params = service.getParameter2Map(request.getParameterMap(), request.getInputStream());
        if (null == params) {
            return service.getPayOutMessage("fail", "失败").toMessage();
        }

        //校验
        if (service.verify(params)) {
            //这里处理业务逻辑
            //......业务逻辑处理块........
            return service.successPayOutMessage(null).toMessage();
        }

        return service.getPayOutMessage("fail", "失败").toMessage();
    }

    @Override
    public String payBackOld(HttpServletRequest request) throws IOException {
        //业务处理在对应的PayMessageHandler里面处理，在哪里设置PayMessageHandler，详情查看com.xx.PayService.setPayMessageHandler()
        return this.buildPayStorageConfig(null).payBack(request.getParameterMap(), request.getInputStream()).toMessage();
    }

    @Override
    public String payBack(HttpServletRequest request) {
        //业务处理在对应的PayMessageHandler里面处理，在哪里设置PayMessageHandler，详情查看com.egzosn.pay.common.api.PayService.setPayMessageHandler()
        return this.buildPayStorageConfig(null).payBack(new HttpRequestNoticeParams(request)).toMessage();
    }
}
